﻿using B.S.BaseData.Domain;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.WriteAPI.Applications.Commands.CarAndPeopleCommands;
using B.S.BaseData.WriteAPI.Applications.Commands.CarCommands;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.BaseData.WriteAPI.Controllers
{
    /// <summary>
    /// 车牌以及事发人管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CarAndPeopleController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        IMediator mediator;
        /// <summary>
        /// 日志
        /// </summary>
        ILogger<CarAndPeopleController> logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">中介者</param>
        /// <param name="logger">日志</param>
        public CarAndPeopleController(IMediator mediator, ILogger<CarAndPeopleController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 车牌添加
        /// </summary>
        /// <param name="car">车牌实体</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>>AddCar(Car car)
        {
            try
            {
                AddCarCommand cmd = new AddCarCommand()
                { data = car };
                logger.LogInformation("车牌添加成功");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError("车牌添加失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 事故人添加
        /// </summary>
        /// <param name="people">事故人实体</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddPeople(People people)
        {
            try
            {
                AddPeopleCommand cmd = new AddPeopleCommand()
                { data = people };
                logger.LogInformation("事故人添加成功");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError("事故人添加失败" + ex.Message);
                throw;
            }
        }
    }
}
